package com.lsk.admin.order;

import com.jfinal.aop.Duang;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.lsk.admin.shop.ShopServices;
import com.lsk.applet.AppletService;
import com.lsk.util.JsonUntil;
import com.lsk.util.PageUtil;
import com.lsk.util.ReturnData;
import com.lsk.util.Syslog;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class OrderController
        extends Controller
{
    OrderService orderService = new OrderService();
    ShopServices shopServices = new ShopServices();
    AppletService appletService = new AppletService();
    OrderService txOrderService = (OrderService)Duang.duang(new OrderService());

    public void shopPayList()
    {
        String state = getPara("state");
        setAttr("state", state);
        renderJsp("shopPayOrderList.jsp");
    }

    public void orderListData()
    {
        String pageStr = getPara("page");

        String limitStr = getPara("limit");

        String state = getPara("state");

        int page = pageStr == null ? 0 : Integer.parseInt(pageStr);

        int limit = limitStr == null ? 0 : Integer.parseInt(limitStr);

        Map map = this.orderService.orderListDataService(page, limit, state);

        renderJson(map);
    }

    public void orderInfo()
    {
        String id = getPara("id");

        Record order = this.orderService.getOrderById(id);

        List<Record> infoList = this.orderService.getOrderInfoList(id);
        Record logistics = this.orderService.getLogisticsInfo(id);
        order.set("infoList", infoList);

        setAttr("order", order);

        setAttr("id", id);

        setAttr("logistics", logistics);

        renderJsp("orderInfo.jsp");
    }

    public void logistics()
    {
        String orderId = getPara("orderId");

        Record logistics = this.orderService.getLogisticsInfo(orderId);

        setAttr("logistics", logistics);
        setAttr("orderId", orderId);
        renderJsp("logistics.jsp");
    }

    public void saveLogistics()
    {
        String id = getPara("id");

        String orderId = getPara("orderId");

        ReturnData returnData = this.orderService.saveLogisticsService(id, orderId);

        renderJson(returnData);
    }

    public void editOrder()
    {
        String id = getPara("id");

        Record order = this.orderService.getOrderById(id);

        Record address = this.orderService.getAddressById(order.getStr("addressId"));

        Syslog.info(address.toJson());

        setAttr("order", order);

        setAttr("id", id);

        setAttr("address", address);

        renderJsp("editOrder.jsp");
    }

    public void delOrder()
    {
        String orderId = getPara("id");
        ReturnData delOrder = this.orderService.delOrder(orderId);
        renderJson(delOrder);
    }

    public void saveEditOrder()
    {
        Map<String, String[]> map = getParaMap();

        ReturnData returnData = this.orderService.saveEditOrderService(map);

        renderJson(returnData);
    }

    public void memberUpVIPData()
    {
        String pageStr = getPara("page");

        String limitStr = getPara("limit");

        int page = pageStr == null ? 0 : Integer.parseInt(pageStr);

        int limit = limitStr == null ? 0 : Integer.parseInt(limitStr);

        Map map = this.orderService.memberUpVIPDataServce(page, limit);

        renderJson(map);
    }

    public void memberUpVIP()
    {
        renderJsp("memberUpVIP.jsp");
    }

    public void orderManagement()
    {
        String page = getPara("page");
        String limit = getPara("limit");
        String startTime=getPara("startTime");
        String overTime=getPara("overTime");
        String checkStatus=getPara("checkStatus");
        String personType=getPara("personType");
        String visitPhone = getPara("visitPhone");
        String checkCode=getPara("checkCode");
        String shop=getPara("shop");
       // String checkStatus=getPara("checkStatus");
       // String fenxiaoName = getPara("fenxiaoName");
        String userId = getSession().getAttribute("userId").toString();
        String spotName = this.shopServices.findSpot(userId);
//        String shopId = this.shopServices.findShop(userId);
        PageUtil pageUtil = this.orderService.orderManagement(page,limit,shop,spotName,visitPhone,checkCode,personType,startTime,overTime,checkStatus);
        JsonUntil jsonUntil = new JsonUntil();
        jsonUntil.setCode("0");
        jsonUntil.setCount(Long.valueOf(pageUtil.getTotalRows()));
        jsonUntil.setMsg("");
        jsonUntil.setData(pageUtil.getList());
        renderJson(jsonUntil);
    }

    public void fenxiaoMember()
    {
        List<Record> list = this.orderService.fenxiaoMember();
        renderJson(list);
    }

    public void delivergoods()
    {
        String orderId = getPara("orderId");
        Record order = this.appletService.getOrderInfoService(orderId);

        order.remove("deliverystatus");
        order.set("deliverystatus", "1");
        order.remove("order");
        order.set("state", "2");

        Db.update("yx_order", order);

        String parentId = "";
        String memberId = (String)order.get("memberId");
        Record memberInfo = this.appletService.getMemberInfoById(memberId);
        if (memberInfo.get("parentId") != null) {
            parentId = (String)memberInfo.get("parentId");
        }
        ReturnData returnData = this.appletService.rebate(parentId, order);
        returnData.setResult(true);
        renderJson(returnData);
    }

    public void checkOrder()
    {
        ReturnData returnData = new ReturnData();


        String orderId = getPara("orderId");
        Record order = this.appletService.getOrderInfoService(orderId);
        order.remove("checkStatus");
        order.set("checkStatus", "0");

        Db.update("yx_order", order);
        returnData.setResult(true);
        renderJson(returnData);

        }



    public void applyRefund()
    {
        String userId = getSession().getAttribute("userId").toString();
        String page = getPara("page");
        String limit = getPara("limit");
        String memberName = getPara("memberName");

        PageUtil pageUtil = this.orderService.applyRefund(page, limit, userId, memberName);
        JsonUntil jsonUntil = new JsonUntil();
        jsonUntil.setCode("0");
        jsonUntil.setCount(Long.valueOf(pageUtil.getTotalRows()));
        jsonUntil.setMsg("");
        jsonUntil.setData(pageUtil.getList());
        renderJson(jsonUntil);
    }
}
